package io.aicn.lowcode.controller;

import io.aicn.lowcode.generator.MybatisPlusCodeGenerator;
import io.aicn.lowcode.generator.model.GenerationRequest;
import io.aicn.lowcode.generator.model.GenerationResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "代码生成器", description = "MyBatis Plus代码生成器API")
@RestController
@RequestMapping("/api/generator")
public class CodeGeneratorController {

    @Autowired
    private MybatisPlusCodeGenerator codeGenerator;

    @Operation(summary = "生成代码", description = "根据表信息生成MyBatis Plus CRUD代码")
    @PostMapping("/generate")
    public GenerationResult generateCode(@RequestBody GenerationRequest request) {
        return codeGenerator.generateCode(request);
    }

    @Operation(summary = "获取数据库表列表", description = "获取当前数据库中的所有表")
    @GetMapping("/tables")
    public List<String> getTableList() {
        return codeGenerator.getTableList();
    }

    @Operation(summary = "获取表结构信息", description = "获取指定表的详细结构信息")
    @GetMapping("/tables/{tableName}")
    public Object getTableInfo(@PathVariable String tableName) {
        try {
            return codeGenerator.getTableInfo(tableName);
        } catch (Exception e) {
            throw new RuntimeException("获取表信息失败: " + e.getMessage());
        }
    }
}